﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Mail;
using CE.Image;
using CE.Global;
using CE.Database;
using CE.User;
using CE.PostCode;
using CE.PersonalAccident;
using CE.Security;
using CE.Provider;
using CE.StreetDirectory;
using CE.Payment;

public partial class personal_accident_hsbc_proposal_form : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        //{
        //    if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
        //        Convert.ToInt32(CE.Global.CEUserType.Customer) && Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
        //        Convert.ToInt32(CE.Global.CEUserType.Provider))
        //    {
        //        Response.Redirect("~/User/LoginAndRegister.aspx");
        //    }
        //}
        //else
        //{
        //    Response.Redirect("~/User/LoginAndRegister.aspx");
        //}
        //if (Session[CE.Global.General.Guest].ToString() == "Provider")
        //{
        //    Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        //}
        if (Session[CE.Global.General.PAResultId] != null)
        {
            if (Session[General.ProviderCompanyId] == null)
            {
                Response.Redirect("~/personal-accident/results.aspx");
            }

            if (Session[General.ProviderCompanyId].ToString() != "23")
            {
                Response.Redirect("~/personal-accident/results.aspx");
            }
        }
        else
        {
            Response.Redirect("~/personal-accident/results.aspx");
        }

        if (!IsPostBack)
        {
            if (Session[General.PAResultId] != null)
            {
                LoadPremium();
            }
            else
            {
                //Response.Redirect("~/personal-accident/personal-accident.aspx");
                Response.Redirect("~/personal-accident/");
            }

            int _year;
            int startyear = CE.Global.General.GetUTCDateTime().Year;
            int endyear = CE.Global.General.GetUTCDateTime().Year + 10;
            for (_year = endyear; _year >= startyear; _year--)
            {
                ddlCardExpiryYear.Items.Insert(0, new ListItem(_year.ToString("0000"), _year.ToString("00")));
            }
            ddlCardExpiryYear.Items.Insert(0, new ListItem("yyyy", "-1"));


            #region Validation Effect

            txtName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtName.ClientID + "','" + rfvName.ClientID + "','" + revName.ClientID + "')");
            txtPostCode.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtPostCode.ClientID + "','" + rfvPostCode.ClientID + "','" + revPostCode.ClientID + "')");
            txtUnit1.Attributes.Add("onblur", "ShowValidationEffect('" + txtUnit1.ClientID + "','" + rfvUnit1.ClientID + "')");
            txtUnit2.Attributes.Add("onblur", "ShowValidationEffect('" + txtUnit2.ClientID + "','" + rfvUnit2.ClientID + "')");
            txtAddress.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtAddress.ClientID + "','" + rfvAddress.ClientID + "')");
            txtEmail.Attributes.Add("onblur", "ShowValidationEffect('" + txtEmail.ClientID + "','" + rfvEmail.ClientID + "','" + revEmail.ClientID + "')");
            txtSpouseName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtSpouseName.ClientID + "','" + rfvSpouseName.ClientID + "','" + revSpouseName.ClientID + "')");
            //txtBank.Attributes.Add("onblur", "ShowValidationEffect('" + txtBank.ClientID + "','" + rfvBank.ClientID + "')");
            //txtChequeNo.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtChequeNo.ClientID + "','" + rfvChequeNo.ClientID + "','" + revChequeNo.ClientID + "')");
            txtNameoftheCard.Attributes.Add("onblur", "ShowValidationEffect('" + txtNameoftheCard.ClientID + "','" + rfvNameoftheCard.ClientID + "')");
            txtIssuingBank.Attributes.Add("onblur", "ShowValidationEffect('" + txtIssuingBank.ClientID + "','" + rfvIssuingBank.ClientID + "')");
            txtCardNo1.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtCardNo1.ClientID + "','" + rfvCardNo1.ClientID + "','" + revCardNo1.ClientID + "')");
            txtCardNo2.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtCardNo2.ClientID + "','" + rfvCardNo2.ClientID + "','" + revCardNo2.ClientID + "')");
            txtCardNo3.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtCardNo3.ClientID + "','" + rfvCardNo3.ClientID + "','" + revCardNo3.ClientID + "')");
            txtCardNo4.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtCardNo4.ClientID + "','" + rfvCardNo4.ClientID + "','" + revCardNo4.ClientID + "')");
            ddlCardExpiryMonth.Attributes.Add("onblur", "ShowValidationEffect('" + ddlCardExpiryMonth.ClientID + "','" + rfvCardExpiryMonth.ClientID + "')");
            ddlCardExpiryYear.Attributes.Add("onblur", "ShowValidationEffect('" + ddlCardExpiryYear.ClientID + "','" + rfvCardExpiryYear.ClientID + "')");
            
            rdoYesEngaged.Attributes.Add("onblur", "CheckEngagedValidationCss();");
            rdoNoEngaged.Attributes.Add("onblur", "CheckEngagedValidationCss();");
            //rdoCashorNets.Attributes.Add("onblur", "CheckPaymentModeValidationCss();");
            //rdoCheque.Attributes.Add("onblur", "CheckPaymentModeValidationCss();");
            //rdoCreditCard.Attributes.Add("onblur", "CheckPaymentModeValidationCss();");
            rdoVisa.Attributes.Add("onblur", "CheckCardTypeValidationCss();");
            rdoMasterCard.Attributes.Add("onblur", "CheckCardTypeValidationCss();");

            #endregion

            Session[General.ViewedDetailsId] = LogUserViewedHistory().ToString();

            if (Session[General.UserId] != null )
            {
                ShowUserDetails();
            }
            else if (Session[General.PAUserName] != null
                && Session[General.PAPhoneNo] != null
                && Session[General.PAEmail] != null
                && Session[General.PAPersonnelId] != null)
            {
                txtEmail.Text = Session[General.PAEmail].ToString();
                txtName.Text = Session[General.PAUserName].ToString();
                txtMobileNo.PhoneNo = Session[General.PAPhoneNo].ToString();
            }
        }
            
        if (rdoYesEngaged.Checked || rdoNoEngaged.Checked) txtEngaged.Text = "1";
        if (chkSPRPR.Checked)
        {
            txtSPRPR.Text = "1";
        }
        else
        {
            txtSPRPR.Text = "";
        }
        if (chkElgibleAge.Checked)
        {
            txtElgibleAge.Text = "1";
        }
        else
        {
            txtElgibleAge.Text = "";
        }
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Session[CE.Global.General.PAResultId] != null)
        {
            if (Session[General.ProviderCompanyId].ToString() != "23")
            {
                Response.Redirect("~/personal-accident/results.aspx");
            }
        }
        else
        {
            Response.Redirect("~/personal-accident/results.aspx");
        }
        CheckValidation();
        ChangeValidationEffectServerSide("proposal");
        if (IsValid)
        {
            Int64 _proposalresult = 0;
            HsbcProposal _hsbcproposal = new HsbcProposal();
            HsbcProposalDB _hsbcproposaldb = new HsbcProposalDB();

            if (Session[General.PAProposalId] != null)
            {
                _hsbcproposal.PAProposalId = Convert.ToInt64(Session[General.PAProposalId].ToString());
            }
            else
            {
                _hsbcproposal.PAProposalId = 0;
            }
            _hsbcproposal.PAId = Convert.ToInt64(Session[General.PAId].ToString());
            _hsbcproposal.PACompanyId = Convert.ToInt64(lblCompanyId.Text.ToString());
            _hsbcproposal.PAInsId = Convert.ToInt64(Session[General.PAResultId].ToString());
            _hsbcproposal.Salutation = ddlSalutation.GetSalutation();
            _hsbcproposal.Name = txtName.Text.ToUpper();
            _hsbcproposal.PostalCode = txtPostCode.Text.Trim();
            _hsbcproposal.UnitNo = txtUnit1.Text.Trim();
            _hsbcproposal.HomeNo = txtUnit2.Text.Trim();
            _hsbcproposal.Address = txtAddress.Text.Trim();
            _hsbcproposal.Gender = ddlGender.GetGender();
            _hsbcproposal.MaritalStatus = Convert.ToInt16(ddlMaritalStatus.GetMaritalStatus());
            _hsbcproposal.NRICNo = ddlIdNumber.GetIdNumber();
            _hsbcproposal.Nationality = ddlNationality.GetNationality();
            _hsbcproposal.MobileNo = txtMobileNo.GetPhoneNo();
            _hsbcproposal.HomePhoneNo = txtHomePhoneNo.GetPhoneNo();
            _hsbcproposal.OfficePhoneNo = txtOfficePhoneNo.GetPhoneNo();
            _hsbcproposal.Email = txtEmail.Text.Trim();
            _hsbcproposal.SpouseName = txtSpouseName.Text.ToUpper();
            _hsbcproposal.SpouseNRICNo = ddlSpouseIdNumber.GetIdNumber();
            _hsbcproposal.SpouseNationality = ddlSpouseNationality.GetNationality();
            _hsbcproposal.SPRPRStatus = chkSPRPR.Checked;
            _hsbcproposal.ElgibleAge = chkElgibleAge.Checked;
            _hsbcproposal.EngagedOccupation = rdoYesEngaged.Checked;
            _hsbcproposal.PamentType = "CreditCard";

            if (Session[CE.Global.General.UserId] != null)
            {
                _hsbcproposal.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
            }
            else if(Session[General.PAPersonnelId] != null)
            {
                _hsbcproposal.RefCEUserId = Convert.ToInt64(Session[General.PAPersonnelId].ToString());
            }
            _hsbcproposal.ViewedDetailId = Convert.ToInt64(Session[General.ViewedDetailsId].ToString());
            _hsbcproposal.Status = true;

            _proposalresult = _hsbcproposaldb.Insert(_hsbcproposal);

            if (_proposalresult > 0)
            {
                Int64 _paymentresult = 0;
                Payment _payment = new Payment();
                PaymentDB _paymentdb = new PaymentDB();

                _payment.CXPaymentId = 0;
                _payment.RefId = Convert.ToInt64(Session[General.PAId].ToString());
                _payment.ProviderCompanyId = Convert.ToInt64(lblCompanyId.Text.ToString());
                _payment.InsuranceType = "PA";
                _payment.NameoftheCard = txtNameoftheCard.Text.ToUpper();
                _payment.IssuingBankName = txtIssuingBank.Text.Trim();
                _payment.CreditCardNo = txtCardNo1.Text.Trim() + "" + txtCardNo2.Text.Trim() + "" + txtCardNo3.Text.Trim() + "" + txtCardNo4.Text.Trim();
                _payment.CardExpiryMonth = ddlCardExpiryMonth.Text;
                _payment.CardExpiryYear = ddlCardExpiryYear.Text;
                if (rdoVisa.Checked == true)
                {
                    _payment.CardType = "Visa";
                }
                else
                {
                    _payment.CardType = "MasterCard";
                }
                _payment.Status = true;
                if (Session[CE.Global.General.UserId] != null)
                {
                    _payment.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                }
                else if(Session[General.PAPersonnelId] != null)
                {
                    _payment.RefCEUserId = Convert.ToInt64(Session[General.PAPersonnelId].ToString());
                }
                _paymentresult = _paymentdb.Insert(_payment);

                if (_paymentresult > 0)
                {
                    ProposalNotification();
                    UserConfirmation _userconfirm = new UserConfirmation();
                    UserConfirmationDB _userconfirmdb = new UserConfirmationDB();
                    _userconfirm.ConfirmationType = "PA";
                    _userconfirm.CurrentStep = 3;
                    _userconfirm.UserSelectedId = Convert.ToInt64(Session[General.PAResultId].ToString());
                    _userconfirm.FirstStepId = Convert.ToInt64(Session[General.PAId].ToString());
                    if (Session[General.UserId] != null)
                    {
                        _userconfirm.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
                    }
                    else if(Session[General.PAPersonnelId] != null)
                    {
                        _userconfirm.RefCEUserId = Convert.ToInt64(Session[General.PAPersonnelId].ToString());
                    }
                    _userconfirm.Status = true;
                    _userconfirm.IsConfirm = true;
                    _userconfirm.ResultStatus = "Complete";
                    _userconfirm.IsComplete = true;
                    Int64 _result = _userconfirmdb.Insert(_userconfirm);

                    if (_result > 0)
                    {
                        string _provideid = Encode("23");
                        General.ClearAllSession(this.Page, "ALL");
                        Response.Redirect("~/personal-accident/usermessage.aspx?pcid=" + _provideid);
                    }
                    else
                    {
                        lblMessage.Text = "Can't update confirmation status.";
                    }
                }
                else
                {
                    lblMessage.Text = "Payment Details Saved Faild.";
                }
            }
        }
    }

    protected void ibtnBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/personal-accident/results.aspx");
    }

    protected void ibtPostCode_Click(object sender, ImageClickEventArgs e)
    {
        GetPostCode();
    }

    protected void txtPostCode_TextChanged(object sender, EventArgs e)
    {
        GetPostCode();
    }

    protected void ddlAddressList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlAddressList.Items.Count > 0 && ddlAddressList.SelectedIndex >= 0)
        {
            txtAddress.Text = ddlAddressList.Text;
        }
    }

    protected void btnPostCode_Click(object sender, EventArgs e)
    {
        txtPostCode_TextChanged(sender, e);
    }

    public string Encode(string sData)
    {
        byte[] encData_byte = new byte[sData.Length];
        encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);
        string encodedData = Convert.ToBase64String(encData_byte);
        return encodedData;
    }

    private void LoadPremium()
    {
        MyDatabase _db = new MyDatabase();
        string strSql = "";
        strSql = @"SELECT CompanyName,PlanName,TotalAnnualPremium,CAST(AnnualPremiumwithGST AS NUMERIC(10,2)) AS AnnualPremiumwithGST,BenefitName,PolicyType,PACompanyId FROM tblProviderPACompareBenefits, ";
        strSql = strSql + " tblProviderCompany WHERE ProviderCompanyId = PACompanyId AND PAInsId = " + Session[CE.Global.General.PAResultId].ToString(); ;
        DataTable _dtResult = new DataTable();
        _dtResult = _db.GetDataTable(strSql);
        if (_dtResult.Rows.Count > 0)
        {
            lblProvider.Text = _dtResult.Rows[0]["CompanyName"].ToString() + " Insurance";
            lblPlanName.Text = _dtResult.Rows[0]["PlanName"].ToString();
            //lblAnnualPremium.Text = _dtResult.Rows[0]["AnnualPremiumwithGST"].ToString();
            lblCompanyId.Text = _dtResult.Rows[0]["PACompanyId"].ToString();
            string _policy = _dtResult.Rows[0]["PolicyType"].ToString();
            txtPolicyType.Text = _dtResult.Rows[0]["PolicyType"].ToString();
            if (_policy == "IND")
            {
                lblPolicyType.Text = "Individual";
                //txtPolicyType.Text = "Individual";
            }
            else if (_policy == "INDSP")
            {
                lblPolicyType.Text = "Individual & Spouse";
                //txtPolicyType.Text = "Individual & Spouse";
            }
            else if (_policy == "INDCHD")
            {
                lblPolicyType.Text = "Individual & Children";
                //txtPolicyType.Text = "Individual & Children";
            }
            else if (_policy == "INDSPCHD")
            {
                lblPolicyType.Text = "Individual, Spouse & Children";
                //txtPolicyType.Text = "Individual, Spouse & Children";
            }

            MyDatabase _db1 = new MyDatabase();
            string strSql1 = "";
            strSql1 = @"SELECT CAST(AnnualPremium AS NUMERIC(10,2)) AS AnnualPremiumwithGST FROM tblPAInsuranceSelectItem WHERE PAId = " + Session[CE.Global.General.PAId].ToString(); ;
            DataTable _dtResult1 = new DataTable();
            _dtResult1 = _db1.GetDataTable(strSql1);
            if (_dtResult1.Rows.Count > 0)
            {
                lblAnnualPremium.Text = _dtResult1.Rows[0]["AnnualPremiumwithGST"].ToString();
            }
        }
        else
        {
            lblProvider.Text = "";
            lblPlanName.Text = "";
            lblAnnualPremium.Text = "";
            lblCompanyId.Text = "0";
            lblPolicyType.Text = "";
            txtPolicyType.Text = "";
        }
        EnableSpousePanels();
    }

    private void CheckValidation()
    {
        txtName.Text = General.ClearScriptTag(txtName.Text);
        txtPostCode.Text = General.ClearScriptTag(txtPostCode.Text);
        txtUnit1.Text = General.ClearScriptTag(txtUnit1.Text);
        txtUnit2.Text = General.ClearScriptTag(txtUnit2.Text);
        txtAddress.Text = General.ClearScriptTag(txtAddress.Text);
        //txtBank.Text = General.ClearScriptTag(txtBank.Text);
        //txtChequeNo.Text = General.ClearScriptTag(txtChequeNo.Text);
        txtNameoftheCard.Text = General.ClearScriptTag(txtNameoftheCard.Text);
        txtIssuingBank.Text = General.ClearScriptTag(txtIssuingBank.Text);
        txtCardNo1.Text = General.ClearScriptTag(txtCardNo1.Text);
        txtCardNo2.Text = General.ClearScriptTag(txtCardNo2.Text);
        txtCardNo3.Text = General.ClearScriptTag(txtCardNo3.Text);
        txtCardNo4.Text = General.ClearScriptTag(txtCardNo4.Text);
        //txtCardExpiryMonth.Text = General.ClearScriptTag(txtCardExpiryMonth.Text);
        //txtCardExpiryYear.Text = General.ClearScriptTag(txtCardExpiryYear.Text);

        #region PolicyType

        if (txtPolicyType.Text == "Individual" || txtPolicyType.Text == "Individual & Children")
        {
            rfvSpouseName.Enabled = false;
            revSpouseName.Enabled = false;
            ddlSpouseIdNumber.IsEnableValidation(false);
            ddlSpouseNationality.IsEnableValidation(false);
            //csvNRIC.Enabled = false;
            
            
        }
        else
        {
            rfvSpouseName.Enabled = true;
            revSpouseName.Enabled = true;
            ddlSpouseIdNumber.IsEnableValidation(true);
            ddlSpouseNationality.IsEnableValidation(true);
            //csvNRIC.Enabled = true;
            
        }
        #endregion

   
    }

    private void ProposalNotification()
    {
        string _strSql, _providerName, _providerEmail, _providerCCEmail, _providerBCCEmail, _Date, _Time, _InsuranceName, _RefNo;
        MyDatabase _db = new MyDatabase();
        DataTable _dtProvider = new DataTable();
        DataTable _dtInsurance = new DataTable();
        _Date = "";
        _Time = "";
        _RefNo = "";
        _strSql = "";
        _providerName = "";
        _providerEmail = "";
        _providerCCEmail = "";
        _providerBCCEmail = "";
        _InsuranceName = "Personal Accident Insurance";
        _strSql = "SELECT [CompanyName], [Email1] FROM tblProviderCompany WHERE ProviderCompanyId = " + lblCompanyId.Text.Trim();
        _dtProvider = _db.GetDataTable(_strSql);
        if (_dtProvider.Rows.Count > 0)
        {
            _providerName = _dtProvider.Rows[0]["CompanyName"].ToString();
            //_providerEmail = _dtProvider.Rows[0]["Email1"].ToString();
            //_providerEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("ProposalNotificationReceiver");
        }

        _providerEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("HSBCPA_TO");
        _providerCCEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("HSBCPA_CC");
        _providerBCCEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("HSBCPA_BCC");

        _strSql = "";
        _strSql = "SELECT CONVERT(VARCHAR,ModifiedDate,103) AS [Date], CONVERT(VARCHAR,ModifiedDate,108) AS [Time] ";
        _strSql += " FROM  tblPAProposal_Hsbc WHERE PAId = " + Session[General.PAId].ToString() + " ORDER BY ModifiedDate DESC";
        _dtInsurance = _db.GetDataTable(_strSql);
        if (_dtInsurance.Rows.Count > 0)
        {
            _Date = _dtInsurance.Rows[0]["Date"].ToString();
            _Time = _dtInsurance.Rows[0]["Time"].ToString();
        }
        //_RefNo = General.GetRefNumber("PA", _Date, Session[General.PAId].ToString());
        string convertno = General.GetRefNumber(Convert.ToInt64(Session[General.PAId].ToString()));
        _RefNo = "PAH" + convertno.ToString();
        if ((_dtProvider.Rows.Count > 0) && (_dtInsurance.Rows.Count > 0))
        {
            MyMailManager.SendProposalCompleteNotification(_providerName, _providerEmail, _providerCCEmail, _providerBCCEmail, _InsuranceName, _Date, _Time, _RefNo);
        }
        
    }

    private Int64 LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        return _userviewdb.LogViewedHistory("PA", Request, "Personal Accident HSBC proposal form.");
    }

    private void EnableSpousePanels()
    {
        if (txtPolicyType.Text.Trim() == "INDSP" || txtPolicyType.Text.Trim() == "INDSPCHD")
        {
            pnlDetailsOfOtherInsured.Visible = true;
            rfvSpouseName.Enabled = true;
            revSpouseName.Enabled = true;
            ddlSpouseIdNumber.IsEnableValidation(true);
            ddlSpouseNationality.IsEnableValidation(true);
            //csvNRIC.Enabled = true;
        }
        else 
        {
            pnlDetailsOfOtherInsured.Visible = false;
            rfvSpouseName.Enabled = false;
            revSpouseName.Enabled = false;
            ddlSpouseIdNumber.IsEnableValidation(false);
            ddlSpouseNationality.IsEnableValidation(false);
            //csvNRIC.Enabled = false;
        }
    }

    private PostCode GetPostCode(string strPostCode)
    {
        PostCode _postcode = new PostCode();
        PostCodeDB _postcodedb = new PostCodeDB();
        _postcode = _postcodedb.GetPostCode(strPostCode);
        _postcodedb = null;
        return _postcode;
    }

    protected void CheckSalGenderValid(object sender, ServerValidateEventArgs args)
    {
        string _sal = ddlSalutation.GetSalutation();
        string _gender = ddlGender.GetGender();
        if (_gender == "M")
        {
            if (_sal == "Ms" || _sal == "Miss" || _sal == "Mdm" || _sal == "Mrs")
            {
                args.IsValid = false;
            }
        }
        else if (_gender == "F")
        {
            if (_sal == "Mr")
            {
                args.IsValid = false;
            }
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void Check2NRICValid(object sender, ServerValidateEventArgs args)
    {
        string _appID = ddlIdNumber.GetIdNumber();
        string _spouseID = ddlSpouseIdNumber.GetIdNumber();

        if (_appID == _spouseID)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }

    }


    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        #region Proposal Details
        //Applicant's Salutation
        ddlSalutation.ChangeValidationEffectServerSide(strValidationGroup);
        //Name
        if (rfvName.Enabled && rfvName.ValidationGroup == strValidationGroup)
        {
            rfvName.Validate();
            if (rfvName.IsValid == false)
            {
                txtName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtName.ControlStyle.CssClass = "";
        }

        if (revName.Enabled && revName.ValidationGroup == strValidationGroup)
        {
            if (rfvName.IsValid)
            {
                revName.Validate();
                if (revName.IsValid == false)
                {
                    txtName.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtName.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtName.ControlStyle.CssClass = "";
        }
        //Postal Code
        if (rfvPostCode.Enabled && rfvPostCode.ValidationGroup == strValidationGroup)
        {
            rfvPostCode.Validate();
            if (rfvPostCode.IsValid == false)
            {
                txtPostCode.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtPostCode.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtPostCode.ControlStyle.CssClass = "";
        }

        if (revPostCode.Enabled && revPostCode.ValidationGroup == strValidationGroup)
        {
            if (rfvPostCode.IsValid)
            {
                revPostCode.Validate();
                if (revPostCode.IsValid == false)
                {
                    txtPostCode.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtPostCode.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtPostCode.ControlStyle.CssClass = "";
        }

        //Unit No
        if (rfvUnit1.Enabled && rfvUnit1.ValidationGroup == strValidationGroup)
        {
            rfvUnit1.Validate();
            if (rfvUnit1.IsValid == false)
            {
                txtUnit1.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtUnit1.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtUnit1.ControlStyle.CssClass = "";
        }

        //Home No
        if (rfvUnit2.Enabled && rfvUnit2.ValidationGroup == strValidationGroup)
        {
            rfvUnit2.Validate();
            if (rfvUnit2.IsValid == false)
            {
                txtUnit2.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtUnit2.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtUnit2.ControlStyle.CssClass = "";
        }

        //Address
        if (rfvAddress.Enabled && rfvAddress.ValidationGroup == strValidationGroup)
        {
            rfvAddress.Validate();
            if (rfvAddress.IsValid == false)
            {
                txtAddress.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtAddress.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtAddress.ControlStyle.CssClass = "";
        }

        //if (revAddress.Enabled && revAddress.ValidationGroup == strValidationGroup)
        //{
        //    if (rfvAddress.IsValid)
        //    {
        //        revAddress.Validate();
        //        if (revAddress.IsValid == false)
        //        {
        //            txtAddress.ControlStyle.CssClass = "validationError";
        //        }
        //        else
        //        {
        //            txtAddress.ControlStyle.CssClass = "";
        //        }
        //    }
        //}
        //else
        //{
        //    txtAddress.ControlStyle.CssClass = "";
        //}

        //Applicant's Gender
        ddlGender.ChangeValidationEffectServerSide(strValidationGroup);

        //Applicant's MaritalStatus
        ddlMaritalStatus.ChangeValidationEffectServerSide(strValidationGroup);

        //Applicant's NRIC Number
        ddlIdNumber.ChangeValidationEffectServerSide(strValidationGroup);

        

        //Applicant's Nationality
        ddlNationality.ChangeValidationEffectServerSide(strValidationGroup);

        //Applicant's Mobile Number
        txtMobileNo.ChangeValidationEffectServerSide(strValidationGroup);

        //Applicant's Home Phone Number
        txtHomePhoneNo.ChangeValidationEffectServerSide(strValidationGroup);

        //Applicant's Office Number
        txtOfficePhoneNo.ChangeValidationEffectServerSide(strValidationGroup);

        //Email
        if (rfvEmail.Enabled && rfvEmail.ValidationGroup == strValidationGroup)
        {
            rfvEmail.Validate();
            if (rfvEmail.IsValid == false)
            {
                txtEmail.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtEmail.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtEmail.ControlStyle.CssClass = "";
        }

        if (revEmail.Enabled && revEmail.ValidationGroup == strValidationGroup)
        {
            if (rfvEmail.IsValid)
            {
                revEmail.Validate();
                if (revEmail.IsValid == false)
                {
                    txtEmail.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtEmail.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtEmail.ControlStyle.CssClass = "";
        }
        //Spouse Name
        if (rfvSpouseName.Enabled && rfvSpouseName.ValidationGroup == strValidationGroup)
        {
            rfvSpouseName.Validate();
            if (rfvSpouseName.IsValid == false)
            {
                txtSpouseName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtSpouseName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtSpouseName.ControlStyle.CssClass = "";
        }

      
        //Spouse NRIC Number
        ddlSpouseIdNumber.ChangeValidationEffectServerSide(strValidationGroup);

        //Spouse Nationality
        ddlSpouseNationality.ChangeValidationEffectServerSide(strValidationGroup);

        //SPR / PR Status
        if (rfvSPRPR.Enabled && rfvSPRPR.ValidationGroup == strValidationGroup)
        {
            rfvSPRPR.Validate();
            if (rfvSPRPR.IsValid == false)
            {
                chkSPRPR.ControlStyle.CssClass = "validationError";
            }
            else
            {
                chkSPRPR.ControlStyle.CssClass = "";
            }
        }
        else
        {
            chkSPRPR.ControlStyle.CssClass = "";
        }

        //Age Eligibility
        if (rfvElgibleAge.Enabled && rfvElgibleAge.ValidationGroup == strValidationGroup)
        {
            rfvElgibleAge.Validate();
            if (rfvElgibleAge.IsValid == false)
            {
                chkElgibleAge.ControlStyle.CssClass = "validationError";
            }
            else
            {
                chkElgibleAge.ControlStyle.CssClass = "";
            }
        }
        else
        {
            chkElgibleAge.ControlStyle.CssClass = "";

        }
        //Insurance Transferred
        if (rfvEngaged.Enabled && rfvEngaged.ValidationGroup == strValidationGroup)
        {
            rfvEngaged.Validate();
            if (rfvEngaged.IsValid == false)
            {
                lblYesEngaged.ControlStyle.CssClass = "validationError";
                lblNoEngaged.ControlStyle.CssClass = "validationError";
            }
            else
            {
                lblYesEngaged.ControlStyle.CssClass = "";
                lblNoEngaged.ControlStyle.CssClass = "";
            }
        }
        else
        {
            lblYesEngaged.ControlStyle.CssClass = "";
            lblNoEngaged.ControlStyle.CssClass = "";
        }

        #endregion

        #region Payment Mode

        
        //Name of the Card
        if (rfvNameoftheCard.Enabled && rfvNameoftheCard.ValidationGroup == strValidationGroup)
        {
            rfvNameoftheCard.Validate();
            if (rfvNameoftheCard.IsValid == false)
            {
                txtNameoftheCard.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtNameoftheCard.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtNameoftheCard.ControlStyle.CssClass = "";
        }
        //Issuing Bank
        if (rfvIssuingBank.Enabled && rfvIssuingBank.ValidationGroup == strValidationGroup)
        {
            rfvIssuingBank.Validate();
            if (rfvIssuingBank.IsValid == false)
            {
                txtIssuingBank.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtIssuingBank.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtIssuingBank.ControlStyle.CssClass = "";
        }
        //Card 1
        if (rfvCardNo1.Enabled && rfvCardNo1.ValidationGroup == strValidationGroup)
        {
            rfvCardNo1.Validate();
            if (rfvCardNo1.IsValid == false)
            {
                txtCardNo1.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtCardNo1.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtCardNo1.ControlStyle.CssClass = "";
        }

        if (revCardNo1.Enabled && revCardNo1.ValidationGroup == strValidationGroup)
        {
            if (rfvCardNo1.IsValid)
            {
                revCardNo1.Validate();
                if (revCardNo1.IsValid == false)
                {
                    txtCardNo1.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtCardNo1.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtCardNo1.ControlStyle.CssClass = "";
        }
        //Card 2
        if (rfvCardNo2.Enabled && rfvCardNo2.ValidationGroup == strValidationGroup)
        {
            rfvCardNo2.Validate();
            if (rfvCardNo2.IsValid == false)
            {
                txtCardNo2.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtCardNo2.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtCardNo2.ControlStyle.CssClass = "";
        }

        if (revCardNo2.Enabled && revCardNo2.ValidationGroup == strValidationGroup)
        {
            if (rfvCardNo2.IsValid)
            {
                revCardNo2.Validate();
                if (revCardNo2.IsValid == false)
                {
                    txtCardNo2.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtCardNo2.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtCardNo2.ControlStyle.CssClass = "";
        }
        //Card 3
        if (rfvCardNo3.Enabled && rfvCardNo3.ValidationGroup == strValidationGroup)
        {
            rfvCardNo3.Validate();
            if (rfvCardNo3.IsValid == false)
            {
                txtCardNo3.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtCardNo3.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtCardNo3.ControlStyle.CssClass = "";
        }

        if (revCardNo3.Enabled && revCardNo3.ValidationGroup == strValidationGroup)
        {
            if (rfvCardNo3.IsValid)
            {
                revCardNo3.Validate();
                if (revCardNo3.IsValid == false)
                {
                    txtCardNo3.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtCardNo3.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtCardNo3.ControlStyle.CssClass = "";
        }
        //Card 4
        if (rfvCardNo4.Enabled && rfvCardNo4.ValidationGroup == strValidationGroup)
        {
            rfvCardNo4.Validate();
            if (rfvCardNo4.IsValid == false)
            {
                txtCardNo4.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtCardNo4.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtCardNo4.ControlStyle.CssClass = "";
        }

        if (revCardNo4.Enabled && revCardNo4.ValidationGroup == strValidationGroup)
        {
            if (rfvCardNo4.IsValid)
            {
                revCardNo4.Validate();
                if (revCardNo4.IsValid == false)
                {
                    txtCardNo4.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtCardNo4.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtCardNo4.ControlStyle.CssClass = "";
        }
        //Card Expiry Month
        if (rfvCardExpiryMonth.Enabled && rfvCardExpiryMonth.ValidationGroup == strValidationGroup)
        {
            rfvCardExpiryMonth.Validate();
            if (rfvCardExpiryMonth.IsValid == false)
            {
                ddlCardExpiryMonth.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlCardExpiryMonth.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlCardExpiryMonth.ControlStyle.CssClass = "";
        }

        //if (revCardExpiryMonth.Enabled && revCardExpiryMonth.ValidationGroup == strValidationGroup)
        //{
        //    if (rfvCardExpiryMonth.IsValid)
        //    {
        //        revCardExpiryMonth.Validate();
        //        if (revCardExpiryMonth.IsValid == false)
        //        {
        //            txtCardExpiryMonth.ControlStyle.CssClass = "validationError";
        //        }
        //        else
        //        {
        //            txtCardExpiryMonth.ControlStyle.CssClass = "";
        //        }
        //    }
        //}
        //else
        //{
        //    txtCardExpiryMonth.ControlStyle.CssClass = "";
        //}
        //Card Expiry Year
        if (rfvCardExpiryYear.Enabled && rfvCardExpiryYear.ValidationGroup == strValidationGroup)
        {
            rfvCardExpiryYear.Validate();
            if (rfvCardExpiryYear.IsValid == false)
            {
                ddlCardExpiryYear.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlCardExpiryYear.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlCardExpiryYear.ControlStyle.CssClass = "";
        }

        //if (revCardExpiryYear.Enabled && revCardExpiryYear.ValidationGroup == strValidationGroup)
        //{
        //    if (rfvCardExpiryYear.IsValid)
        //    {
        //        revCardExpiryYear.Validate();
        //        if (revCardExpiryYear.IsValid == false)
        //        {
        //            txtCardExpiryYear.ControlStyle.CssClass = "validationError";
        //        }
        //        else
        //        {
        //            txtCardExpiryYear.ControlStyle.CssClass = "";
        //        }
        //    }
        //}
        //else
        //{
        //    txtCardExpiryYear.ControlStyle.CssClass = "";
        //}
        //CardType
        if (rfvCardType.Enabled && rfvCardType.ValidationGroup == strValidationGroup)
        {
            rfvCardType.Validate();
            if (rfvCardType.IsValid == false)
            {
                lblVisa.ControlStyle.CssClass = "validationError";
                lblMasterCard.ControlStyle.CssClass = "validationError";
            }
            else
            {
                lblVisa.ControlStyle.CssClass = "";
                lblMasterCard.ControlStyle.CssClass = "";
            }
        }
        else
        {
            lblVisa.ControlStyle.CssClass = "";
            lblMasterCard.ControlStyle.CssClass = "";
        }

        #endregion
    }

    protected void csvCreditCardValid_ServerValidate(object source, ServerValidateEventArgs args)
    {
        bool _status = true;
        string _first = txtCardNo1.Text.Trim().Substring(0, 1);
        if (rdoVisa.Checked)
        {
            if (_first != "4")
            {
                _status = false;
            }
        }
        else if (rdoMasterCard.Checked)
        {
            if (_first != "5")
            {
                _status = false;
            }
        }
        if (_status == true)
        {
            txtCreditCardValid.Text = "";
            txtCreditCardValid.Text = txtCardNo1.Text.Trim() + "" + txtCardNo2.Text.Trim() + "" + txtCardNo3.Text.Trim() + "" + txtCardNo4.Text.Trim();
            string _cardNumer = txtCreditCardValid.Text;
            MySecurity _mysecurity = new MySecurity();
            if (_mysecurity.IsCreditCardValid(_cardNumer))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
        else
        {
            args.IsValid = false;
        }
    }

    protected void csvCCExpiredValid_ServerValidate(object source, ServerValidateEventArgs args)
    {
        txtCCExpiredValid.Text = "";
        txtCCExpiredValid.Text = "valid";
        Int32 _Year = Convert.ToInt32(ddlCardExpiryYear.Text);
        Int32 _Month = Convert.ToInt32(ddlCardExpiryMonth.Text);

        MySecurity _mysecurity = new MySecurity();
        if (_mysecurity.IsCreditCardExpiredDateValid(_Year, _Month))
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }

    private void ShowUserDetails()
    {
        //hplEditProfile.Visible = true;
        CE.User.Profile _profile = new CE.User.Profile();
        CE.User.ProfileDB _profiledb = new ProfileDB();
        CE.User.Contact _contact = new Contact();
        CE.User.ContactDB _contactdb = new ContactDB();

        Int64 UserId = Convert.ToInt64(Session[General.UserId].ToString());
        _profile = _profiledb.GetProfile(UserId);
        if (_profile != null)
        {
            txtName.Text = _profile.FirstName + " " + _profile.SurName;

            if (_profile.IDNo.Length == 9)
            {
                ddlIdNumber.Prefix = _profile.IDNo[0].ToString();
                ddlIdNumber.Postfix = _profile.IDNo[8].ToString();
                ddlIdNumber.IDNumber = _profile.IDNo.Substring(1, 7);
            }

            if (_profile.Gender.ToString() != "")
            {
                ddlGender.Gender = _profile.Gender.ToString();
            }

            if (_profile.Nationality != "")
            {
                ddlNationality.Nationality = _profile.Nationality.ToString();
            }

            _contact = _contactdb.GetContact(UserId);
            if (_contact != null)
            {
                txtMobileNo.PhoneNo = _contact.Mobile;
                txtOfficePhoneNo.PhoneNo = _contact.OfficePhone;
                txtEmail.Text = _contact.Email.ToString();

            }

            _profile = null;
            _profiledb = null;
            _contact = null;
            _contactdb = null;
        }


    }

    private void GetPostCode()
    {
        rfvPostCode.Validate();
        revPostCode.Validate();
        if (rfvPostCode.IsValid && revPostCode.IsValid)
        {
            txtPostCode.ControlStyle.CssClass = "";
            txtAddress.ControlStyle.CssClass = "";
            MyLocation _myLoc = new MyLocation();
            //string _strAddress = _myLoc.GetAddressByPostCode(txtPostCode.Text);
            //if (_strAddress != null && _strAddress != string.Empty)
            //{
            //    if (_strAddress.ToLower().Contains("invalid api"))
            //    {
            //        txtAddress.Text = "Address can't be retrieved. Please type your address detail here.";
            //    }
            //    else
            //    {
            //        txtAddress.Text = _strAddress;
            //    }
            //}
            //else
            //{
            //    txtAddress.Text = "Address is not found.";
            //}

            System.Data.DataSet _ds = _myLoc.GetAddressByPostCodeDataSet(txtPostCode.Text);
            ddlAddressList.Items.Clear();
            string _lstValue;
            string _lstText;
            int _rowCount = 0;

            if (_ds != null)
            {
                if (_ds.Tables.Count > 0)
                {
                    foreach (System.Data.DataTable _dt in _ds.Tables)
                    {
                        if (_dt.Rows.Count > 0 && _dt.Columns.Count >= 2)
                        {
                            foreach (System.Data.DataRow _dr in _dt.Rows)
                            {
                                //lblAddress2.Text += _dr["t"].ToString() + "<br />";
                                //lblAddress2.Text += _dr["a"].ToString() + "<br /><br />";

                                if (_dr["t"] != null && _dr["a"] != null)
                                {
                                    _lstValue = _dr["t"].ToString() + ", " + _dr["a"].ToString();
                                    _lstText = _dr["t"].ToString();
                                    ddlAddressList.Items.Add(new ListItem(_lstText, _lstValue));
                                    if (_rowCount == 0)
                                    {
                                        txtAddress.Text = _lstValue;
                                        //ibtPostCode.Enabled = false;
                                        txtAddress.ReadOnly = true;
                                    }

                                    _rowCount++;
                                }
                            }
                        }
                        else
                        {
                            txtAddress.Text = "Confirm postcode or enter manually.";
                            txtAddress.ReadOnly = false;
                            txtAddress.ControlStyle.CssClass = "validationError";
                        }
                    }
                }
                else
                {
                    txtAddress.Text = "Confirm postcode or enter manually.";
                    txtAddress.ReadOnly = false;
                    txtAddress.ControlStyle.CssClass = "validationError";
                }
            }
            else
            {
                txtAddress.Text = "Please type your address here.";
                txtAddress.ReadOnly = false;
                txtAddress.ControlStyle.CssClass = "validationError";
            }

            if (_rowCount > 1)
            {
                divAddressList.Visible = true;
            }
            else
            {
                divAddressList.Visible = false;

            }
        }
        else
        {
            txtPostCode.ControlStyle.CssClass = "validationError";
            ddlAddressList.Items.Clear();
            divAddressList.Visible = false;
            txtAddress.Text = "Invalid postcode.";
            txtAddress.ControlStyle.CssClass = "validationError";
        }
    }

}
